package edu.gatech.wms.views;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import edu.gatech.wms.R;
import edu.gatech.wms.controllers.ItemManager;
import edu.gatech.wms.controllers.Login;

public class ItemsFragment extends ListFragment
{
	private Set<Integer>			ids;
	private List<String>			names, types;
	private ArrayAdapter<String>	adapter;
	private SparseIntArray			pos;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		ids = ItemManager.getAllItems();
		names = new ArrayList<String>();
		types = new ArrayList<String>();
		pos = new SparseIntArray();
		
		for (int id : ids)
		{
			pos.put(names.size(), id);
			
			names.add(ItemManager.getItemName(id));
			types.add(""+ItemManager.getItemType(id));
		}

		adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
				names);
		setListAdapter(adapter);
		
		View view = inflater.inflate(R.layout.item_list, null);
		view.findViewById(R.id.item_list_filter).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				AlertDialog dialog;

				final String[] items = { "Electronic", "Photo", "Jewelry", "Furniture",
						"Toy", "Other" };
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
				builder.setTitle("Categories");
				builder.setItems(items, new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int position)
					{
						ids = ItemManager.queryItems(items[position]);
						names = new ArrayList<String>();
						adapter.clear();
						pos = new SparseIntArray();
						for (int id : ids)
						{
							pos.put(names.size(), id);
							String item = ItemManager.getItemName(id) + String.format("%15s", ItemManager.getItemType(id).toString());
							names.add(item);
							adapter.add(item);
						}
					}
				});
				dialog = builder.create();
				dialog.show();
			}
		});
		
		view.findViewById(R.id.item_list_filter_type).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				AlertDialog dialog;

				final String[] items = { "Lost", "Found", "Donated" };
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
				builder.setTitle("Item Type");
				builder.setItems(items, new DialogInterface.OnClickListener()
				{
					public void onClick(DialogInterface dialog, int position)
					{
						ids = ItemManager.queryItemsByType(position);
						names = new ArrayList<String>();
						adapter.clear();
						pos = new SparseIntArray();
						for (int id : ids)
						{
							pos.put(names.size(), id);
							String item = ItemManager.getItemName(id) + String.format("%15s", ItemManager.getItemType(id).toString());
							names.add(item);
							adapter.add(item);
						}
					}
				});
				dialog = builder.create();
				dialog.show();
			}
		});

		view.findViewById(R.id.item_list_search).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				EditText text = (EditText) getActivity().findViewById(R.id.item_list_search_bar);
				ids = ItemManager.queryItems(text.getText().toString());
				names = new ArrayList<String>();
				adapter.clear();
				pos = new SparseIntArray();
				for (int id : ids)
				{
					String item = ItemManager.getItemName(id) + String.format("%15s", ItemManager.getItemType(id).toString());
					pos.put(names.size(), id);
					names.add(item);
					adapter.add(item);
				}
			}
		});

		return view;
	}

	@Override
	public void onResume()
	{
		super.onResume();

		ids = ItemManager.getAllItems();
		names = new ArrayList<String>();
		pos = new SparseIntArray();
		
		for (int id : ids)
		{
			pos.put(names.size(), id);
			String name = ItemManager.getItemName(id);  
			names.add(name);
		}
		
		adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,
				names);
		setListAdapter(adapter);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id)
	{
		Intent intent = new Intent(getActivity(), EditItemActivity.class);
		intent.putExtra(Login.ITEM_KEY, pos.get(position));
		getActivity().startActivityForResult(intent, 1);
	}
}